<?php
/////////////////////////////////////////////////////////////////////////////
// 
//
// workflow_system
//
// workflow_copyright
// 
/////////////////////////////////////////////////////////////////////////////

/**
 * FILE_NAME : shopproductcategory.class.php   FILE_PATH : \\classes\application\shopproductcategory.class.php
 * ....商铺宝贝分类
 *
 * @copyright workflow_system 
 * @author workflow Develop Team 
 * @package 
 * @subpackage 
 * @version Fri Sep 07 12:13:49 CST 2007
 */

class ShopProductCategoryClass extends FrameWork {
	/**
	 * 新增商铺宝贝分类
	 *
	 * @param var[] $input_param
	 * @return boolen
	 */
	function _addProductCategory($input_param){
		if (!empty($input_param)){
			$arr = array();
			if (is_array($input_param)){
				foreach ($input_param as $k => $v){
					if (trim($v) != ''){
						$arr[$k] = $v;
					}
				}
			}
			return $GLOBALS['db']->InsertRow($arr,"shop_product_class","class_id");
		}else {
			return false;
		}
	}

	/**
	 * 修改商铺宝贝分类
	 *
	 * @param var[] $inptu_param
	 * @return boolen
	 */
	function _updateProductCategory($input_param){
		if (is_array($input_param)){
			$arr = array();
			foreach ($input_param as $k => $v){
				$arr[$k] = $v;
			}
		}
		$insert_rs = $GLOBALS['db']->UpdateRow($arr['class_id'],$arr, 'shop_product_class', 'class_id');
		return $insert_rs;
	}

	/**
	 * 删除商铺宝贝分类
	 *
	 * @param var $input_id 分类ID，可以是某个分类ID，也可以是分类ID数组
	 * @return boolen
	 */
	function delOperateCategory($input_id){
		if (is_array($input_id)){
			foreach ($input_id as $value){
				$this->_deleteCategory($value);
			}
		}else{
			$this->_deleteCategory($input_id);
		}
		return true;
	}

	/**
	 * 删除某个商铺某个分类
	 *
	 * @param int $input_id 宝贝分类ID
	 * @param int $shop_id 商铺ID
	 * @return boolen
	 */
	function _deleteCategory($input_id){
		if (intval($input_id) > 0){
			$del_rs = $GLOBALS['db']->DeleOneRow(intval($input_id),'shop_product_class','class_id');
			//移除该分类下的商品
			$value_array = array();
			$value_array['p_class_id'] = '0';
			$GLOBALS['db']->UpdateRow(intval($input_id),$value_array,'product','p_class_id',true);
		}
		return true;
	}

	/**
	 * 得到某个（所有）商店的宝贝分类
	 *
	 * @param int $shop_id
	 */
	function getCategory($condition,&$obj_page){
		$substr = $this->_conditionSearch($condition);
		$category_row = $GLOBALS['db']->GetJoinList($obj_page,array('shop_product_class','shop_info'),"left join",array("shop_product_class.shop_id=shop_info.shop_id"),array('*','shop_name'),$substr);
		return $category_row;
	}
	
	/**
	 * 将类别数组整理为多级数组形式
	 * @author workflow Develop Team     
	 * @param  $class_array 类别数组
	 * @return array
	 */
	function _makeCategoryArray($class_array){
		//整理排序 将2级类别放到一级类别下
		if (!empty($class_array)){
			foreach($class_array as $v) {
				$v['child'] = array(); //给每个节点附加一个child项
				if($v['class_parent_id'] == 0) {
					$i = count($d);
					$d[$i] = $v;
					$ind[$v['class_id']] = &$d[$i];
				}else {
					$i = count($ind[$v['class_parent_id']]['child']);
					$ind[$v['class_parent_id']]['child'][$i] = $v;
					$ind[$v['class_id']] =& $ind[$v['class_parent_id']]['child'][$i];
				}
			}
			return $d;
		}else {
			return false;
		}
	}
	
	/**
	 * 构造检索条件
	 * @author workflow Develop Team     
	 * @param  $condition 检索条件
	 * @return string
	 */
	function _conditionSearch($condition){
		$condition_str = '';
		
		//店铺ID
		if ($condition['shop_id'] != ''){
			$condition_str .= " and shop_product_class.shop_id = '" . $condition['shop_id'] . "'";
		}
		//店铺名称模糊查找
		if ($condition['like_shop_name'] != ''){
			$condition_str .= " and shop_info.shop_name like '%" . $condition['like_shop_name'] . "%'";
		}
		//类别父ID
		if ($condition['class_parent_id'] != ''){
			$condition_str .= " and shop_product_class.class_parent_id = '" . $condition['class_parent_id'] . "'";
		}
		//排序 
		if ($condition['order_by'] != ''){
			$condition_str .= " order by ".$condition['order_by'];
		}
		return $condition_str;
	}
	
	/**
	 * 取该类别信息
	 * @author workflow Develop Team     
	 * @param  $class_id 类别ID
	 * @return array
	 */
	function getOneCategory($class_id){
		if (intval($class_id) > 0){
			$class_array = $GLOBALS['db']->GetOneRow(intval($class_id),'shop_product_class','class_id');
			return $class_array;
		}else {
			return false;
		}
	}
	
	/**
	 * 转移类别下到另一个类别中
	 * @author workflow Develop Team     
	 * @param  $move_array需要转移的ID数组,$to_id转入的类别ID
	 * @return bool
	 */
	function moveShopProductClass($move_array,$to_id){
		if (intval($to_id) > 0 && !empty($move_array)){
			foreach ($move_array as $k => $v){
				if ($v != $to_id){
					$condition_str[] = "class_id = '" . $v . "'";
					$condition_str[] = "class_parent_id = '" . $v . "'";
				}
			}
			$condition_str = implode(' or ',$condition_str);
			$value_array = array();
			$value_array['class_parent_id'] = intval($to_id);
			if (substr(trim($condition_str),0,3) == 'and'){
				$condition_str = substr(trim($condition_str),3);
			}
			$result = $GLOBALS['db']->UpdateRows('shop_product_class',$value_array,$condition_str);
			unset($condition,$condition_str,$value_array);
			return $result;
		}
		return false;
	}
}
?>